<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Categories_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Categories_model extends Base_model {
	function Categories_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );
		
		$this->load->module_model( "article", "category_model" );
		
		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Events_model class loaded' );
	}

	/**
	 * Get Categories
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getCategories( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'event_type.id, 
							event_type.category_id,
							event_type.category_language_id,
							event_type.description,
							event_type.title' );
		
		$this->db->from( "event_type" );

		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('event_type.id desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Insert new Category
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//get category_language_id by category_id
		$data['category_language_id'] = $this->category_model->getCategoryLanguageId( $data['category_id'] );
		//get category_id
		$data['category_id'] = $this->category_model->getCategoryID( $data['category_id'] );
	
		//create new events entry
		$this->db->set( 'id',          $data['id']         	);
		$this->db->set( 'title',       $data['title']      	);
		$this->db->set( 'description', $data['description'] );
		$this->db->set( 'category_id', $data['category_id'] );
		$this->db->set( 'category_language_id', $data['category_language_id'] );

		//insert the new event type
		$this->db->insert( 'event_type' );
		
		return $this->db->insert_id( );
	}
	
	/**
	 * Update category
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {
		//get category_language_id by category_id
		$data['category_language_id'] = $this->category_model->getCategoryLanguageId( $data['category_id'] );
			
		$events = array( );
		$events['title']       = $data['title'];
		$events['description'] = $data['description'];
		$events['category_id'] = $data['category_id'];
		$events['category_language_id'] = $data['category_language_id'];

		//insert the new event type
		$this->db->update( 'event_type', $events, array( 'id' => $id ) );
	}

	/**
	 * Constructs an array of the given categories to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildCategoriesDropdown( ) {
		$categories = $this->getCategories( );
		
		$dropdown = array( );
		foreach( $categories->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->title;
		}
		
		return $dropdown;
	}

	/**
	 * Get Category ID
	 *
	 * @access public
	 * @param int $id Category id
	 * @return int
	 */
	/*function getTypeID( $id ) {
		$this->db->select( 'id' );
		$this->db->from( "event_type" );
		$this->db->where( "id = " . $id );
		
		$query = $this->db->get( );
		foreach( $query->result( ) as $row ) {
   			$event_type_id = $row->id;
		}
		return $event_type_id;
	}*/
}
/* End of file categories_model.php */
/* Location: ./modules/events/models/categories_model.php */